package com.example.java_chatroom_redis_rabbitmq.utils;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitUtil {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RabbitAdmin rabbitAdmin;
    @Autowired
    private Queue messageQueue;

    public void createExchange(String name) {
        FanoutExchange exchange = new FanoutExchange(name + ".exchange");
        rabbitAdmin.declareExchange(exchange);
        Binding binding = new Binding("message.queue", Binding.DestinationType.QUEUE,name+".exchange"
        ,"message",null);
        Binding binding2 = new Binding("message.queue", Binding.DestinationType.EXCHANGE,name+".exchange"
                ,"message",null);
        rabbitAdmin.declareBinding(binding);
    }

    public void sendMessage(String exchangeName, String routingKey,String json) {
        rabbitTemplate.convertAndSend(exchangeName,routingKey,json);
    }
}
